//
//  IBFireWorks.m
//  O2
//
//  Created by apple on 15/7/9.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "IBFireWorksView.h"
#import <CoreGraphics/CoreGraphics.h>

@interface IBFireWorksView ()
{
    CAEmitterLayer * _fireEmitterLayer;
}
@end

@implementation IBFireWorksView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self animationEmitter];
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

+(Class)layerClass
{
    return  [CAEmitterLayer class];
}

-(void)animationEmitter
{
    _fireEmitterLayer = (CAEmitterLayer *)self.layer;
    _fireEmitterLayer.emitterPosition = CGPointMake(self.bounds.size.width/ 2, self.bounds.size.height);    // 坐标
    _fireEmitterLayer.emitterSize = CGSizeMake(self.bounds.size.width * 2 / 3, 0);               // 粒子显示层宽度
    _fireEmitterLayer.renderMode = kCAEmitterLayerAdditive;      // 递增渲染模式
    _fireEmitterLayer.emitterMode = kCAEmitterLayerOutline;      // 粒子发射模式（向线外发射）
    _fireEmitterLayer.emitterShape = kCAEmitterLayerLine;        // 粒子形状（线）
    _fireEmitterLayer.seed = (arc4random()%100) + 1;
    
    //爆炸前的移动星星圆粒子
    CAEmitterCell *cycleCell = [CAEmitterCell emitterCell];
    cycleCell.birthRate = 1.5;
    cycleCell.lifetime = 0.75;
    cycleCell.emissionLatitude = 0;
    cycleCell.emissionLongitude = 0;
    cycleCell.emissionRange = M_PI_4/2;    // 发射角度范围 上半圆
    cycleCell.velocity = self.bounds.size.height + 10; //上移的路线
    cycleCell.velocityRange = 0.1;
    cycleCell.contents = (id)[[UIImage imageNamed:@"fireCycle.png"] CGImage];
    cycleCell.scale = 0.05;
    
    // 爆炸时的粒子
    CAEmitterCell *burstCell = [CAEmitterCell emitterCell];
    burstCell.birthRate	= cycleCell.birthRate;
    burstCell.scale = 2.5; //星星的大小变化规模
    burstCell.lifetime = 0.2;
    
    // 爆炸后的散射星星例子
    CAEmitterCell *starCell = [CAEmitterCell emitterCell];
    starCell.birthRate = 400;
    starCell.velocity = 100;
    starCell.lifetime = 1;
    starCell.lifetimeRange = 0.5;
    starCell.emissionRange = M_PI;    // 发射角度范围
    starCell.yAcceleration = 75;
    starCell.contents = (id)[[UIImage imageNamed:@"fireStar.png"] CGImage];
    starCell.color = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1].CGColor;
    starCell.alphaSpeed = -0.8;
    starCell.scale = 2;
    starCell.scaleRange = 0.1;
    starCell.spin = 2*M_PI;;
    starCell.redRange = 0.5;
    starCell.greenRange = 0.5;
    starCell.blueRange = 0.5;
    
    //爆炸后的烟花效果
    CAEmitterCell* smokeCell = [CAEmitterCell emitterCell];
    [smokeCell setName:@"smoke"];
    
    smokeCell.birthRate			= 10;
    smokeCell.emissionLongitude = -M_PI / 2;
    smokeCell.lifetime			= 10;
    smokeCell.velocity			= -40;
    smokeCell.velocityRange		= 20;
    smokeCell.emissionRange		= M_PI / 4;
//    smoke.spin				= 1;
//    smoke.spinRange			= 6;
    smokeCell.yAcceleration		= - 100;
    smokeCell.contents			= (id) [[UIImage imageNamed:@"fireSmoke.png"] CGImage];
    smokeCell.scale				= 0.1;
    smokeCell.alphaSpeed		= -0.9;
    smokeCell.scaleSpeed		= 0.7;
    
    _fireEmitterLayer.emitterCells = @[cycleCell];
    cycleCell.emitterCells = @[burstCell];
    burstCell.emitterCells = @[starCell,smokeCell];
}



@end
